<?php

namespace App\Jobs;

use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;

class SettlementBatchPayJob implements ShouldQueue
{
    use Batchable;
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private string $settlementId;

    private string $settlementNumber;

    private int $tenantId;

    private int $initiatorId;

    private string $bankName = 'BankOfHangzhou';

    private int $maxQueryAttempts = 10;

    private int $queryIntervalSeconds = 30;

    public function __construct(string $settlementId, string $settlementNumber, int $tenantId, int $initiatorId)
    {
        $this->settlementId = $settlementId;
        $this->settlementNumber = $settlementNumber;
        $this->tenantId = $tenantId;
        $this->initiatorId = $initiatorId;
    }

    public function middleware(): array
    {
        return [
            new WithoutOverlapping(sprintf('settlement-batch-pay-%s', $this->settlementId)),
        ];
    }

    public function handle(): void
    {
        Artisan::call('settlement:batch-pay', [
            'settlementId' => $this->settlementId,
            'settlementNumber' => $this->settlementNumber,
            'tenantId' => $this->tenantId,
            'initiatorId' => $this->initiatorId,
            '--bank' => $this->bankName,
            '--max-query-attempts' => $this->maxQueryAttempts,
            '--query-interval' => $this->queryIntervalSeconds,
        ]);
    }
}
